package io.renren.modules.generator.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.GrassEntity;
import io.renren.modules.generator.service.GrassService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 草场数据管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-04-18 17:30:13
 */
@RestController
@RequestMapping("grass")
public class GrassController {
	@Autowired
	private GrassService grassService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("grass:list")
	public R list(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);

		List<GrassEntity> grassList = grassService.queryList(query);
		int total = grassService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(grassList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{grassId}")
	@RequiresPermissions("grass:info")
	public R info(@PathVariable("grassId") Integer grassId) {
		GrassEntity grass = grassService.queryObject(grassId);

		return R.ok().put("grass", grass);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("grass:save")
	public R save(@RequestBody GrassEntity grass) {
		grassService.save(grass);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("grass:update")
	public R update(@RequestBody GrassEntity grass) {
		grassService.update(grass);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("grass:delete")
	public R delete(@RequestBody Integer[] grassIds) {
		grassService.deleteBatch(grassIds);

		return R.ok();
	}

	@RequestMapping("/namelist")
	@RequiresPermissions("grass:list")
	public R namelist(@RequestParam Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);

		List<GrassEntity> nppList = grassService.nameList(query);
		int total = grassService.nameTotal(query);

		PageUtils pageUtil = new PageUtils(nppList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
		//return R.ok(starttime);
	}
}
